//
//  LDYnetWorkTools.swift
//  swift封装单例工具
//
//  Created by 李东阳 on 16/4/18.
//  Copyright © 2016年 李东阳. All rights reserved.
//


import UIKit
import AFNetworking
enum methedTools : Int{
    case GET = 0
    case POST = 1
}


class LDYnetWorkTools: AFHTTPSessionManager {
    

    static let sharedTools:LDYnetWorkTools = {
        let tools = LDYnetWorkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        return tools
        }()
    
    

        //MARK: -  网络请求
    func request(methed: methedTools , urlString: String, parm: AnyObject?, callSucces: (response:AnyObject?, error:NSError? ) ->() ){
    
//      let manager = AFHTTPSessionManager()
        
        if methed == .GET{
            self.GET(urlString, parameters: parm, progress: nil, success: { (_, response) -> Void in
                callSucces(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    callSucces(response: nil, error: error)
                    
                    
                    
                    
            })
            // POST
        }else{
            self.POST(urlString, parameters: parm, progress: nil, success: { (_, response) -> Void in
                callSucces(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    callSucces(response: nil, error: error)
                    
            })
        }
        
    
    }
}



//MARK: - 请求微博首页数据
extension LDYnetWorkTools{
    
    func loadStatus(accessToken:String,since_id: Int64, max_id: Int64, callSucces: (response:AnyObject?, error:NSError? ) ->() ){
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        
        //MARK: -  请求参数
        let parm = [
            "access_token":accessToken,
            "since_id":"\(since_id)",
            "max_id":"\(max_id)"
            
        ]
        //发送请求
       request(methedTools.GET, urlString: urlString, parm: parm) { (response, error) -> () in
        callSucces(response: response, error: error)
        
        }
    }
    
}


